# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def flatten(self, root):
        """
        :type root: TreeNode
        :rtype: None Do not return anything, modify root in-place instead.
        """
        if not root:
            return root
        node = root
        while True:
            while node.left:
                succesor = node.left
                while succesor.right:
                    succesor = succesor.right
                succesor.right = node.right
                node.right = node.left
                node.left = None
                node = node.right
            if node.right:
                node = node.right
            else:
                break
        return root